package com.hnevc.wangyu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hnevc.wangyu.R;
import com.hnevc.wangyu.bean.UserBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView title_back;
    private TextView title_text;
    private EditText et_name;
    private EditText et_password;
    private CheckBox cb_login;
    private Button btn_login;
    private TextView tv_wangji_pwd;
    private TextView tv_wangji_sgin;
//    private SharedPreferences sp;
    private String name;
    private String password;
    private SharedPreferences.Editor editor;

    private static final String TAG = "LoginActivity";
    private Gson gson;
    private UserBean userBean;
    private Object RequestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {

//        cb_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked == true){
//                    Toast.makeText(LoginActivity.this, "记住密码", Toast.LENGTH_SHORT).show();
//                    editor.putString("name",name);
//                    editor.putString("password",password);
//                    editor.putBoolean("isbln",isChecked);
//                    editor.commit();
//
//                }else {
//                    Toast.makeText(LoginActivity.this, "取消记住密码", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
//        tv_wangji_pwd.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                Toast.makeText(LoginActivity.this, "忘记密码！", Toast.LENGTH_SHORT).show();
//                editor.remove("name");
//                editor.remove("password");
//                editor.commit();
//            }
//        });
//
//        String dataname = sp.getString("name","")==null?"" : sp.getString("name","");
//        String datapassord = sp.getString("password","")==null?"" : sp.getString("password","");
//        et_name.setText(dataname);
//        et_password.setText(datapassord);

    }

    private void initData() {
        String url = "http://anli-wechat.boruiman.com/freshmenapp/user/login";
        String nameStr = et_name.getText().toString().trim();
        String passwordStr = et_password.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name",nameStr);
            jsonObject.put("password",passwordStr);
        }catch (JSONException e) {
            e.printStackTrace();
        }



    }

    private void initView() {
        title_back = (ImageView) findViewById(R.id.title_back);
        title_text = (TextView) findViewById(R.id.title_text);
        title_text.setText("用户登录");
        et_name = (EditText) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);
        cb_login = (CheckBox) findViewById(R.id.cb_login);
        btn_login = (Button) findViewById(R.id.btn_login);
        tv_wangji_pwd = (TextView) findViewById(R.id.tv_wangji_pwd);
        tv_wangji_sgin = (TextView) findViewById(R.id.tv_wangji_sgin);


        title_back.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        tv_wangji_sgin.setOnClickListener(this);

//        sp = getSharedPreferences("data",MODE_PRIVATE);
//        editor = sp.edit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                Toast.makeText(this, "目前还没有上一页", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_login:
                if (submit()==true) {
                    getlogin();
            }
                break;
            case R.id.tv_wangji_sgin:
                startActivity(new Intent(LoginActivity.this,RegistActivity.class));
                break;
        }
    }

    private void getlogin(){
        new Thread(new Runnable() {
            @Override
            public void run() {

                final String url = "http://anli-wechat.boruiman.com/freshmenapp/user/login";
                OkHttpClient client = new OkHttpClient();

                RequestBody formbody = new FormBody.Builder()
                        .add("name",name)
                        .add("password",password)
                        .build();
                final Request request = new Request.Builder()
                        .url(url)
                        .post(formbody)
                        .build();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: "+e.getMessage() );
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        if (!response.isSuccessful()){
                            Log.e(TAG, "onResponse: 请求失败 "+ response.message() );
                        }
                        Log.e(TAG, "onResponse: "+response );
                        gson = new Gson();
                        userBean = gson.fromJson(response.body().string(), UserBean.class);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (userBean.getMsg().equals("成功")){
                                    startActivity(new Intent(LoginActivity.this,ClassmateListActivity.class));
                                }
                                Toast.makeText(LoginActivity.this, "登录"+userBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                });

            }
        }).start();
    }

    private boolean submit() {
        // validate
        name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "用户名", Toast.LENGTH_SHORT).show();
            return false;
        }

        password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}